<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<?php
if (count($_POST['nums']) < 1 && (! $_POST['alllek'])) {
	header('Location: Tutorial.php');
}
?>
<head>
	<title>Colton’s Greek Reader</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="flashcard.css" rel="stylesheet" type="text/css">
</head>

<?php

try {
	$db = new PDO('sqlite:colton.db3');
	$sentences = array();

	if ($_POST['alllek']) {
		$q = $db->query("SELECT question, answer FROM questions ORDER BY RANDOM() LIMIT 30;");
		$sentences = $q->fetchAll(PDO::FETCH_NUM);
	} else {
		$xn = min($_POST['nums']);
		$q = $db->query("SELECT question, answer FROM questions WHERE number < $xn ORDER BY RANDOM() LIMIT 15;");
		$sentences = array_merge($sentences, $q->fetchAll(PDO::FETCH_NUM));
		foreach($_POST['nums'] as $num) {
			$q = $db->query("SELECT question, answer FROM questions WHERE number=$num ORDER BY RANDOM();");
			$sentences = array_merge($sentences, $q->fetchAll(PDO::FETCH_NUM));
		}
	}
	
	$db = NULL;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

?>
<script type="text/javascript">
<?php
print "var sentences = new Array(".count($sentences).");\n";
for($i = 0; $i < count($sentences); $i++) {

	$tmp = implode("', '", $sentences[$i]);

	print "sentences[$i] = ['$tmp'];\n";
}
?>

var shown = 0;

function updateVoc() {
	var i = Math.floor(shown / 2);

	var j = shown % 2;

	if (j == 0) {
		document.getElementById('v1').innerHTML = sentences[i][0];
		document.getElementById('v3').innerHTML = "";
	} else if (j == 1) {
		document.getElementById('v1').innerHTML = sentences[i][0];
		document.getElementById('v3').innerHTML = sentences[i][1];
	}
}

function next() {
	shown++;
	if (shown > sentences.length * 2 - 1) {
		shown = 0;
		alert('All done!');
	}
	updateVoc();
}

function prev() {
	shown--;
	if (shown < 0) {
		shown = sentences.length * 2 - 1;
		alert('All done!');
	}
	updateVoc();
}


</script>

<body onload="updateVoc()">

<h1 style="text-align:center">Colton’s Greek Reader</h1>



<div class="center">

<div id="v1" class="gk"></div>
<div id="v2" class="v2">&nbsp;</div>
<div id="v3" class="v3"></div>

<a href="#" class="button" id="btnprev" onclick="prev()" accesskey="p">&lt;&lt;</a>
<a href="#" class="button" id="btnnext" onclick="next()" accesskey="n">&gt;&gt;</a>
<a href="#" class="button" id="btnnew" onclick="location.reload();">new</a>


</div>

</body>
</html>
